package controllers;

import play.*;
import play.mvc.*;

import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.commons.mail.EmailException;

import models.*;

@With(Secure.class)					//this class is used for the login, Doaa El-Hawary
public class Comments extends Controller{

	 public static void index() {
	        render();
	    }


	 	
	 
		/**
		 * Adds a new comment to the database then binds it with the model it is added to (depending on the type of model, having its ID passed as a parameter)
		 *
		 * @author      Arwa Ismail
		 * @version     2011.0503                            
		 * @category   C4S8
		 * @params     username: the name of the user currently having the session,
		 * 			   content: the comment itself
		 * 			   type: the type of model that the comment is added to
		 * 			   spam: whether the comment is a spam or not
		 * 			   modelID: the ID of the model commented on
		 */
		public static void addComment(String username, String content, String type, boolean spam, int modelID) {

			Calendar cal = Calendar.getInstance();
			
			User sender = User.findByUsername(username);
			
			Comment comment = new Comment(sender, content, type, false, modelID);

			if (type.equalsIgnoreCase("Idea")) {
				Idea idea = Idea.findById(modelID);
				idea.getComments().add(comment);
			} else

			{
				if (type.equalsIgnoreCase("Plan")) {
					Plan plan = Plan.findById(modelID);
					plan.getComments().add(comment);
				} else {
					if (type.equalsIgnoreCase("Request")) {
						Request request = Request.findById(modelID);
						request.getComments().add(comment);
					}
				}
			}

			render();

		}
		
		

		/**
		 *  view for comment
		 *
		 * @author   Aly Mostafa   
		 * @version    5-5-2011                          
		 * @category   C3
		 * @params    comment id
		 */
		public static void viewComment(long id){
Comment c = Comment.findById(id);
		String con = c.getContent();	
render(c,con);
		}
		
		/**
		 *  view all  comments on a topic
		 *
		 * @author   Aly Mostafa   
		 * @version    5-5-2011                          
		 * @category   C3
		 * @params    topic id
		 */
			public static void viewAllCommentsonTopic(long topic){
		Topic t = Topic.findById(topic);
				List<Comment> list2 = t.getComments();
				
				List<String> list = new ArrayList<String>();
				for (int i = 0; i <list2.size(); i++) {
					list.add(list2.get(i).getContent());
				}
				render(list);
			}


/**
 *  view all  comments on a Idea
 *
 * @author   Aly Mostafa   
 * @version    5-5-2011                          
 * @category   C3
 * @params    Idea id
 */
	public static void viewAllCommentsonIdea(long idea){
Idea t = Idea.findById(idea);
		List<Comment> list2 = t.getComments();
		List<String> list = new ArrayList<String>();
		for (int i = 0; i <list2.size(); i++) {
			list.add(list2.get(i).getContent());
		}
		
		render(list);
	}

	 /**
	 * if the comment is already spammed it notifies the user of that and if not it sends notification 
	 * 
	 * @author Nora zakhary
	 * @version 2011.0504
	 * @category C3S7
	 * @param long id
	 * @return void
	 */

public static void toSpamComment(long id) throws EmailException{
	Comment c = Comment.findById(id);
    boolean notSpammed;
	if(c.isSpam()){
		notSpammed=false;
	}
	else{
	c.setSpam(true);
	notSpammed= true;
	c.save();
	Users.notifySpammedIdea(id);
	
	}
	
 render(id,notSpammed);
}


}
